#include "usermanagement.h"
#include "ui_usermanagement.h"
#include "sqlcontroller.h"
#include "register.h"
#include <QMessageBox>
#include <QSqlRecord>
#include <maincontroller.h>
#include <QDebug>
#include <QTimer>
UserManagement::UserManagement(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::UserManagement)
{
    ui->setupUi(this);


    model = SqlController::getSqlController()->getModelUserManage();
    ui->tableView->setModel(model);
    ui->tableView->verticalHeader()->setHidden(true);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

UserManagement::~UserManagement()
{
    delete ui;
}
//刷新
void UserManagement::refresh()
{
     model = SqlController::getSqlController()->getModelUserManage();
     ui->tableView->setModel(model);
     ui->tableView->verticalHeader()->setHidden(true);
     ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

}

//退出
void UserManagement::on_pushButton_4_clicked()
{   MainController::getMainController()->showMainWindow();
    this->close();
}

//注册
void UserManagement::on_pushButton_clicked()
{    MainController::getMainController()->hideMainWindow();
     MainController::getMainController()->showRegister();

//      QTimer::singleShot(30*1000,this,[=](){
//          refresh();
//      });


//    this->show();
}

//删除
void UserManagement::on_pushButton_2_clicked()
{
    int row = ui->tableView->currentIndex().row();
    if(row >= 0)
    {
        QSqlRecord record = SqlController::getSqlController()->getModelUserManage()->record(row);
        int id = record.value(0).toInt();
//        qDebug()<<"id "<<id;
        bool ret = SqlController::getSqlController()->removeUser(id);
        if(ret){
            QMessageBox::information(this,"删除结果:","删除成功！");
        } else QMessageBox::information(this,"删除结果:","删除失败！");
    }
    refresh();

}


//修改
void UserManagement::on_pushButton_3_clicked()
{


    if (model->submitAll())
    {
    QMessageBox::information(this, tr("tableModel"),tr("数据修改成功！"));
    } else
    {
    QMessageBox::information(this, tr("tableModel"),tr("数据修改失败！"));
    }
   refresh();

}

